package com.xinsoft.controller.device;

import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.CusTaskDTO;
import com.xinsoft.entity.dto.CusTaskParam;
import com.xinsoft.entity.dto.CusTaskTransferDTO;
import com.xinsoft.entity.po.CusTaskApprove;
import com.xinsoft.entity.po.CusTaskDeal;
import com.xinsoft.entity.vo.*;
import com.xinsoft.service.CusTaskService;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cus/task")
@ApiOperation("任务事件")
public class CusTaskController {
    @Autowired
    private CusTaskService cusTaskService;

    /**
     * 分页
     * @folder 任务事件/所有任务
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody CusTaskParam param) {
        return Response.succeed(cusTaskService.findPageListByParam(param));
    }

    /**
     * 分页1
     * @folder 任务事件/所有任务
     */
    @PostMapping("/pageList1")
    public CusTaskPageVo pageList1(@RequestBody CusTaskParam param) {
        return null;
    }

    /**
     * 暂存
     * @folder 任务事件/所有任务
     */
    @PostMapping("/addOrUpdate")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> addOrUpdate(@RequestBody CusTaskDTO dto) {
        ValidatorUtils.validateEntity(dto);
        cusTaskService.tempSave(dto);
        return Response.succeed();
    }

    /**
     * 提交
     * @folder 任务事件/所有任务
     */
    @PostMapping("/submit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> submit(@RequestBody CusTaskDTO dto) {
        ValidatorUtils.validateEntity(dto);
        cusTaskService.submit(dto);
        return Response.succeed();
    }

    /**
     * 获取详情
     * @folder 任务事件/所有任务
     */
    @GetMapping("/detail")
    public Response<CusTaskVo> detail(@RequestParam(value = "id", defaultValue = "") Integer id) {
        return Response.succeed(cusTaskService.detail(id));
    }

    /**
     * 批量删除
     * @folder 任务事件/所有任务
     */
    @PostMapping("/batchDelete")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        ids = ids.stream().distinct().collect(Collectors.toList());
        cusTaskService.del(ids);
        return Response.succeed();
    }


    /**
     * 处理-暂存
     * @folder 任务事件/待处理
     */
    @PostMapping("/dealTS")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> dealTS(@RequestBody CusTaskDeal taskDeal) {
        ValidatorUtils.validateEntity(taskDeal);
        cusTaskService.dealTS(taskDeal);
        return Response.succeed();
    }

    /**
     * 处理-提交
     * @folder 任务事件/待处理
     */
    @PostMapping("/dealSubmit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> dealSubmit(@RequestBody CusTaskDeal taskDeal) {
        ValidatorUtils.validateEntity(taskDeal);
        cusTaskService.dealSubmit(taskDeal);
        return Response.succeed();
    }

    /**
     * 转派
     * @folder 任务事件/待处理
     */
    @PostMapping("/transfer")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> transfer(@RequestBody CusTaskTransferDTO dto) {
        cusTaskService.transfer(dto.getTaskId(), dto.getUserId());
        return Response.succeed();
    }

    /**
     * 最新一条处理中的任务处理详情
     * @folder 任务事件/待处理
     * @param taskId 任务id
     * @return {@link Response}<{@link CusTaskDealVo}>
     */
    @GetMapping("/getNewestDealTS")
    public Response<CusTaskDealVo> getNewestDealTS(Integer taskId){
        return Response.succeed(cusTaskService.getNewestDealTS(taskId));
    }

    /**
     * 验收-暂存
     * @folder 任务事件/待验收
     */
    @PostMapping("/acceptanceTS")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> acceptanceTS(@RequestBody CusTaskApprove cusTaskApprove) {
        ValidatorUtils.validateEntity(cusTaskApprove);
        cusTaskService.acceptanceTS(cusTaskApprove);
        return Response.succeed();
    }

    /**
     * 验收-提交
     * @folder 任务事件/待验收
     */
    @PostMapping("/acceptanceSubmit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> acceptanceSubmit(@RequestBody CusTaskApprove cusTaskApprove) {
        ValidatorUtils.validateEntity(cusTaskApprove);
        cusTaskService.acceptanceSubmit(cusTaskApprove);
        return Response.succeed();
    }

    /**
     * 获取暂存的验收信息
     * @folder 任务事件/待验收
     * @param taskId 任务id
     * @return {@link Response}<{@link CusTaskDealVo}>
     */
    @GetMapping("/getAcceptanceTS")
    public Response<CusTaskApproveVo> getAcceptanceTS(Integer taskId){
        return Response.succeed(cusTaskService.getAcceptanceTS(taskId));
    }

    /**
     * 任务处理详情
     *
     * @param taskDealId 任务处理id
     * @folder 任务事件/所有任务
     */
    @GetMapping("/dealDetail")
    public Response<CusTaskDealDetailVo> dealDetail(@ApiParam("任务处理id") @RequestParam(value = "taskDealId", defaultValue = "") Integer taskDealId) {
        return Response.succeed(cusTaskService.dealDetail(taskDealId));
    }
}
